/**********************************************************
 * AJAXAction.java.java
 * Created by Ashutosh
 * Last modified Date: Sep 18, 2008 10:44:25 AM by Ashutosh
 * Revision: <svn revision #>
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.action;

import java.io.InputStream;
import java.io.StringBufferInputStream;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.printeverywhere.delegator.LocationCategoriesServiceDelegator;
import com.printeverywhere.delegator.UserServiceDelegator;
import com.printeverywhere.dto.LocationCategories;

// TODO: Auto-generated Javadoc
/**
 * The Class AJAXAction.
 */
public class AJAXAction extends ActionSupport implements ServletRequestAware, ServletResponseAware {

	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(AJAXAction.class);

	 /** The request. */
 	private HttpServletRequest request;

     /** The response. */
     private HttpServletResponse response;

     /** The input stream. */
     private InputStream inputStream;

     /**
      * Gets the input stream.
      *
      * @return the input stream
      */
     public InputStream getInputStream() {
          return inputStream;
     }

     /**
      * Gets the country.
      *
      * @return the country
      *
      * @throws Exception the exception
      */
     public String getCountry() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getCountry() - start"); //$NON-NLS-1$
		}

		int regionId = Integer.parseInt(request.getParameter("showCountry"));

	       StringBuffer result = new StringBuffer("");
	       result.append("<list>\n");
	       List<LocationCategories> countryList = LocationCategoriesServiceDelegator.getCountryListByRegionId(regionId);
	       int totalCountry = countryList.size();
	       result.append(" <option value=\"0\">" + "Select Country " + "</option>\n");
	       if (countryList != null) {
	           for (int index = 0; index < totalCountry; index++) {
	        	   result.append(" <option value=\"" + ((LocationCategories) countryList.get(index)).getCategoryId() +"\">" + ((LocationCategories) countryList.get(index)).getCategoryName() + "</option>\n");
	           }
	       }
	       result.append("</list>\n");
	       inputStream = new StringBufferInputStream(result.toString());

		if (logger.isDebugEnabled()) {
			logger.debug("getCountry() - end"); //$NON-NLS-1$
		}
	       return SUCCESS;
	  }

     /**
      * Gets the states.
      *
      * @return the states
      *
      * @throws Exception the exception
      */
     public String getStates() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getStates() - start"); //$NON-NLS-1$
		}

	       int countryId = Integer.parseInt(request.getParameter("showStates"));
	       StringBuffer result = new StringBuffer("");
	       result.append("<list>\n");
	       List<LocationCategories> statesList = LocationCategoriesServiceDelegator.getStateListByCountryId(countryId);
	       int totalStates = statesList.size();

	       if (statesList != null) {
	           for (int index = 0; index < totalStates; index++) {
	        	   result.append(" <option value=\"" + ((LocationCategories) statesList.get(index)).getCategoryId() +"\">" + ((LocationCategories) statesList.get(index)).getCategoryName() + "</option>\n");
	           }
	       }
	       result.append("</list>\n");

	       inputStream = new StringBufferInputStream(result.toString());

		if (logger.isDebugEnabled()) {
			logger.debug("getStates() - end"); //$NON-NLS-1$
		}
	       return SUCCESS;
	  }



     public String getCountryListingOptions() throws Exception {
 		if (logger.isDebugEnabled()) {
 			logger.debug("getCountriesForList() - start"); //$NON-NLS-1$
 		}

 	       int regionId = Integer.parseInt(request.getParameter("showCountry"));
 	       StringBuffer result = new StringBuffer("");
 	       result.append("<select name=\"listingOptions.defaultCountry\">\n");
 	       List<LocationCategories> statesList = LocationCategoriesServiceDelegator.getCountryListByRegionId(regionId);
 	       int totalStates = statesList.size();

 	       if (statesList != null) {
 	           for (int index = 0; index < totalStates; index++) {
 	        	   result.append(" <option value=\"" + ((LocationCategories) statesList.get(index)).getCategoryId() +"\">" + ((LocationCategories) statesList.get(index)).getCategoryName() + "</option>\n");
 	           }
 	       }
 	       result.append("</select>\n");
 	       inputStream = new StringBufferInputStream(result.toString());

 		if (logger.isDebugEnabled()) {
 			logger.debug("getCountriesForList() - end"); //$NON-NLS-1$
 		}
 	       return SUCCESS;
 	  }
     /* (non-Javadoc)
      * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
      */
     public void setServletRequest(HttpServletRequest request) {
         this.request = request;
    }

    /* (non-Javadoc)
     * @see org.apache.struts2.interceptor.ServletResponseAware#setServletResponse(javax.servlet.http.HttpServletResponse)
     */
    public void setServletResponse(HttpServletResponse response) {
         this.response = response;
    }
}
